package com.jt.common.thread;

public class SingletonTests {
    //多线程调用该单例对象
    public static void main(String[] args) {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getSingleton());
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getSingleton());
            }
        };
        Thread t3 = new Thread(){
            @Override
            public void run() {
                System.out.println(Singleton.getSingleton());
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}
class Singleton{
    /**
     * 单例设计模式-懒汉式加载
     */
    private Singleton(){};
    private static Singleton singleton;
    public static Singleton getSingleton(){
        if(singleton == null){
            synchronized (singleton){
                if(singleton == null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
